import copy
from lib2to3.btm_utils import reduce_tree

print(type(4))
print(type(4.0))
print(type(4.0 == 4.0))

print(int("11", 2))
print(float("4.32"))
print(complex(3, 4))
print(bin(3))
print(oct(3))
print(hex(3))
print(chr(97))
print(tuple("sssdfasdf"))
print(list((1, 2, 3)))
print({1, 2, 3})

str1 = "白晨大大"
print(str1)
print(type(str1))

byte = str1.encode("utf-8")
print(byte)
print(type(byte))

str1 = byte.decode("utf-8")
print(str1)
print(type(str1))

int1 = 10
float1 = 3.14159
str1 = "int1 = %d, float1 = %f" % (int1, float1)
print(str1)  # int1 = 10, float1 = 3.141590

int1 = 10
float1 = 3.14159
bool1 = True
str2 = "int1 = {}, float1 = {}, bool1 = {}".format(int1, float1, bool1)
print(str2)  # int1 = 10, float1 = 3.14159, bool1 = True

str2 = "{:*^20,.2f}".format(float1)
print(str2)

print("{0}'s position is {{0}}".format(10))

str3 = f"int1 = {int1}, float1 = {float1}, bool1 = {bool1}"
print(str3)
str3 = f"{int1 = }, {float1 = }, {bool1 = }"
print(str3)
str3 = f"{{int1 = }}, {{float1 = }}, {{bool1 = }}"
print(str3)

print(8 // 5)
print(3 ** 2)

num1 = 20
print((num2 := 3 ** 2) > num1)
print(num2)

num1 = 1
num2 = 20
test_list = [1, 2, 3, 4, 5]
print(num1 in test_list)
print(num2 not in test_list)

m = 10
n = 10
q = 30
print(m is n)
print(m is not q)

q = [1, 2, 3]
b = q
print(q is b)
print(q == b)

b = q[:]
print(q is b)
print(q == b)

num1 = 10
num2 = 20
num3 = num1 if num1 > num2 else num2
print(num3)

for i in [1, 2, 3]:
    print(i)

for i in "hello":
    print(i)

for i in range(-5, 5, 3):
    print(i)
print(test_list[::-1])

list1 = [100, 200]
list1.append(300)
list1.insert(2, 400)
print(list1)
list2 = ['a', 'b', 'c']
print(list1 + list2)
print(list1 * 2)
list1[0:] = [111, 222, 333]
print(list1)
print(111 in list1)
print(max(list1))
print(sum(list1))
print(min(list1))

for i in range(len(list1)):
    print(i, list1[i])

for i, val in enumerate(list1):
    print(i, val)

del list1[0]
print(list1)

list1.append(222)
list1.remove(222)
print(list1)
print(list1.pop())
print(list1)

list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for inner_list in list1:
    print(inner_list)

squares = [x ** 2 for x in range(10)]
print(squares)
even_squares = [x ** 2 for x in range(10) if x % 2 == 0]
print(even_squares)
list1 = [1, 2, 3]
squares = [x ** 2 for x in list1]
print(squares)
list2 = ['a', 'b', 'c', 'd']
squares = [(i, j) for i in list1 for j in list2]
print(squares)

zipped = zip(list1, list2)
print(zipped)
print(list(zipped))

str1 = "hello, world"
print("he" in str1)
print(r"hello\nworld")
str1.replace("world", "Python")
print(str1)
print(str1.split(","))
print(str1.join(list2))

print(str1.find("world"))
# print(str1.index("lll"))

tuple1 = (1,)
tuple1 = (1, 2, 3)
print(id(tuple1), tuple1)
tuple1 = tuple1 * 2
print(id(tuple1), tuple1)

set1 = {1, 2, 3, 4}
set2 = set([3, 4, 5, 6])
set3 = set1 | set2
print(set1, set2, set3)
set3 = set1 & set2
print(set3)
set3 = set1 ^ set2
print(set3)
set3 = set1 - set2
print(set3)

set1.add(5)
print(set1)

set1.remove(3)
print(set1)

dict1 = {"name": "白晨大大", "age": 18}
print(dict1)
dict2 = dict(name="白晨大大", age=18)
print(dict2)
print(id(dict1), id(dict2))
dict1["age"] = 20
print(id(dict1), dict1)
print(dict2["name"], dict2.get("name"))
print(dict2.get("xx", "不存在"))
# print(dict2["xx"])

dict3 = {x: x ** 2 for x in range(5)}
print(dict3)
print(dict3.keys())
print(dict3.values())
print(dict3.items())

dict3["age"] = 20
print(dict3)
print("age" in dict3)
print(20 in dict3)

del dict3["age"]
print(dict3)


def printInfo(num, *args, **kwargs):
    print(num)
    print(args)
    print(kwargs)


# printInfo(1, 2, 3, 4, name="白晨大大", age=18)
printInfo(1, *tuple1, **dict1)


def func1(a, b, /, c, d, *, e, f):
    print(a, b, c, d, e, f)


func1(1, 2, 3, 4, e=5, f=6)


def deepcopyTest(li):
    """演示深拷贝"""
    li[3].append(300)
    print("in func:", li)


list1 = [1, 2, 3, [100, 200]]
print(deepcopyTest(copy.deepcopy(list1)))
print("out func:", list1)

def testReturn():
    return 1, 2, 3, (4, 5)

print(testReturn())

a = int(3.9)  # 内建作用域 (Python本身提供的,在所有位置都可以访问)
b = 0  # 全局作用域
def outer():
    c = 1  # 嵌套作用域
    def inner():
        d = 2  #局部作用域
        print(d,c,b,a)
    return inner
in_func=outer()
in_func()

num = 2
if num > 1:
    msg = "helloWorld"
print(msg)

def test():
    msg_test = "welcome"
# print(msg_test)

var1 = 100
def global_test():
    global var1
    var1 = 200
    print(var1)
global_test()

list1 = [1, 2, 3]
def modify_list(li):
    li.append(4)
    print("in func:", li)
modify_list(list1)

def function_outer():
    var1 = 1
    print(var1)
    def function_inner():
        nonlocal var1
        var1 = 2
        print(var1)
    function_inner()
    print(var1)
function_outer()

def add(a:int, b:(1, 100)) -> int:
    return a + b
print(add.__annotations__)

def calc(a, b, func):
    return func(a, b)
print(calc(3, 5, add))

print(calc(3, 5, lambda a, b: a + b))

student_list = [{"name": "zhang3", "age": 36}, {"name": "li4", "age": 14}, {"name": "wang5", "age": 27}]
print(sorted(student_list, key=lambda x: x["age"]))

map_result = map(lambda x: x ** 2, range(5))
print(list(map_result))
filter_result = filter(lambda x: x % 2 == 0, range(10))
print(list(filter_result))

from functools import reduce
reduce_result = reduce(lambda x, y: x * y, range(5))
print(reduce_result)

file = open("test.txt", "w", encoding="utf-8")
file.write("hello world")
file.close()

file = open("test.txt", "r", encoding="utf-8")
print(file.read(5))
print(file.readline())
file.close()